#include "stdafx.h"
#include "detail/bmock_mutex.hpp"

namespace bmock {
	bmock_mutex* bmock_mutex::instance_ = NULL;

	bmock_mutex::~bmock_mutex() {
		for (; locks_ > 0; --locks_) {
			m_.unlock();
		}
		instance_ = NULL;
	}

	bmock_mutex* bmock_mutex::instance() {
		if (instance_ == NULL) {
			instance_ = new bmock_mutex();
		}
		return instance_;
	}

	void bmock_mutex::lock() { 
		m_.lock(); 
		++locks_;
	}

	void bmock_mutex::unlock() { 	
		--locks_;
		m_.unlock(); 	
	}

	void bmock_mutex::destruct() {
		if (instance_ != NULL) {
			delete instance_;	
		}
	}
}